﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

using Matchingo.Common.Constants;
using Matchingo.Common.UIHelpers;

namespace Matchingo.Modules.BackgroundModule.Views
{
    /// <summary>
    /// The View for the Background Module - used to display the backdrop images in the game
    /// </summary>
    public partial class BackgroundView : UserControl, IBackgroundView
    {
        private BackgroundViewModel ViewModel { get; set; }

        public BackgroundView()
        {
            InitializeComponent();
        }

        public void Initialize(BackgroundViewModel model)
        {
            ViewModel = model;

            DataContext = ViewModel;
        }


        #region ========== Public Methods ==========

        /// <summary>
        /// This method will transition to a given background from the current background 
        /// </summary>
        /// <param name="newBackground">The URI of the new background to transition to</param>
        public void ChangeVisibleImage(Uri newBackground)
        {
            // to similate a fading transition we will:

            // 1) set the hidden background image to the old image
            BackgroundImageDuplicate.Source = BackgroundImage.Source;
            BackgroundImageDuplicate.Visibility = Visibility.Visible;
            BackgroundImageDuplicate.Opacity = 1.0;

            // 2) set the main background image to 0 opacity
            BackgroundImage.Opacity = 0;
            BackgroundImage.Visibility = Visibility.Visible;

            // 3) set the main background image to the new image
			BackgroundImage.Source = new BitmapImage(newBackground);

            // 4) fade back in the main background image
            Storyboard sb = TransparencyHelper.BuildTransparencyTransition(BackgroundImage,
                1.00D, CommonConstants.UI_TRANSITIONS_DEFAULT_BACKGROUND_CHANGE_DURATION, false);

            // 5) hide the temp bitmap again
            sb.Completed += (s, e) => { BackgroundImageDuplicate.Visibility = Visibility.Collapsed; };
            sb.Begin();
        }

        #endregion
    }
}
